package lanou3g.myfirstapp36kr.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;

import java.util.Timer;
import java.util.TimerTask;

import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.base.BaseActivity;

/**
 * Created by dllo on 16/8/19.
 */
public class LaucherActivity extends BaseActivity {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;



    @Override
    protected int getLayout() {
        return R.layout.activity_lancher;
    }

    @Override
    protected void initView() {
        findViewById(R.id.iv_lancher).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LaucherActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        sharedPreferences = this.getSharedPreferences("check",MODE_PRIVATE);
        editor = sharedPreferences.edit();
        final Intent itToWel = new Intent(this,WelcomeActivity.class);
        final Intent itToMain = new Intent(this,MainActivity.class);
        Timer timer = new Timer();
        firstLoadJudge(timer,itToWel,itToMain);

    }

    private void firstLoadJudge(Timer timer, Intent itToWel, Intent itToMain) {
        boolean firstLoad = sharedPreferences.getBoolean("firstLoad",true);
        if (firstLoad){
            delayToWel(timer,itToWel);
            editor.putBoolean("firstLoad",false);
            editor.commit();
        }else {
            delayToMain(timer,itToMain);
        }
    }

    private void delayToMain(Timer timer, final Intent itToMain) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                startActivity(itToMain);
            }
        };
        timer.schedule(task,1000 * 1);



    }

    private void delayToWel(final Timer timer, final Intent itToWel) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                startActivity(itToWel); //执行进入
            }
        };
        timer.schedule(task, 1000 * 1);//1秒后就进入welcome


    }

    @Override
    protected void initData() {





    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
